package com.test.mybatis.generator;

import com.alibaba.fastjson.JSON;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.*;

/**
 * @author HelSing
 * @date 2019/3/18
 */
public class MybatisCommentGenerator {
    private static String database = "fkdx";
    private static String className = "com.mysql.jdbc.Driver";

    private static String username = "root";
    private static String password = "gdhg0Nsrc6Y$hkqi";

    private static String url = "jdbc:mysql://47.95.220.176:3306/" + database + "?serverTimezone=GMT";
    private static List<String> c = new ArrayList<>();
    static {
        //c.add("m_advertisement");
        //c.add("card_register");

    }

    public static void main(String[] args) throws Exception {
        //front();
        swagger();
    }

    public static void front() throws Exception{
        List<String> cz = new ArrayList<>();
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, username, password);
        DatabaseMetaData metaData = connection.getMetaData();

        //获取数据库下面所有表
        ResultSet tables = metaData.getTables(database, null, "%", new String[] { "TABLE" });

        while (tables.next()) {
            // 列的个数
            int columnCount = tables.getMetaData().getColumnCount();
            // 根据指定列名称获取数据
            String TABLE_NAME = tables.getString("TABLE_NAME") ;
            if (cz.contains(TABLE_NAME)) {
                continue;
            }
            if (!c.isEmpty()) {
                if (!c.contains(TABLE_NAME)) {
                    continue;
                }
            }
            System.out.println(TABLE_NAME);
            cz.add(TABLE_NAME);
            // User表  获取表的所有列
            ResultSet columns = metaData.getColumns("yfgranary", "%", TABLE_NAME, "%");

            // 表格
            List<TableHeader> tableList = new ArrayList<TableHeader>();

            // 表单
            List<FromAddColumn> columnList = new ArrayList<FromAddColumn>();
            StringBuilder json = new StringBuilder();
            while (columns.next()) {
                String COLUMN_NAME = columns.getString("COLUMN_NAME");
                COLUMN_NAME = convertName(COLUMN_NAME);
                String TYPE_NAME = columns.getString("TYPE_NAME");
                String REMARKS = columns.getString("REMARKS");
                String COLUMN_SIZE = columns.getString("COLUMN_SIZE");
                if (COLUMN_NAME.equals("yn")) {
                    continue;
                }
                json.append(",\"").append(COLUMN_NAME).append("\":null");
                TableHeader th = new TableHeader();
                th.setProp(COLUMN_NAME);
                th.setLabel(REMARKS.contains(" ") ? REMARKS.split(" ")[0]: REMARKS);
                if (COLUMN_NAME.equals("id")) {
                    th.setLabel("编码");
                }
                th.setWidth("150");
                tableList.add(th);
                FromAddColumn f = new FromAddColumn();
                f.setField(COLUMN_NAME);
                f.setName(REMARKS.contains(" ") ? REMARKS.split(" ")[0]: REMARKS);
                if (COLUMN_NAME.equals("id")) {
                    f.setName("编码");
                } else if ("DATETIME".equals(TYPE_NAME)) {
                    f.setDate(true);
                    f.setPlaceholder("请选择时间");
                } else if (("BIT".equals(TYPE_NAME) || "TINYINT".equals(TYPE_NAME)) && COLUMN_SIZE.equals("1")) {
                    String str[] = REMARKS.split(" ");
                    List<Map<String, String>> selectList = new ArrayList<>();
                    for (int i = 1, l = str.length; i < l; i++) {
                        String s = str[i];
                        if (s.trim().equals("") || s.trim().equals("默认")){
                            continue;
                        }
                        Map<String, String> m = new HashMap<>();
                        String[] d = s.split(":");
                        if(d.length > 1) {
                            m.put("val", d[0]);
                            m.put("label", d[1]);
                            selectList.add(m);
                        }
                    }
                    f.setSelectData(selectList);
                }
                columnList.add(f);
            }
            System.out.println("{" + json.toString().substring(1) + "}");

            System.out.println(JSON.toJSONString(tableList));
            System.out.println(JSON.toJSONString(columnList));
        }

    }

    private static String convertName(String columnName) {
        StringBuilder sb = new StringBuilder();
        String[] str = columnName.split("_");
        sb.append(str[0]);
        for (int i = 1, l = str.length; i < l; i++) {
            String s = str[i];
            sb.append(s.substring(0,1).toUpperCase(Locale.ROOT));
            sb.append(s.substring(1));
        }
        return sb.toString();
    }

    public static void swagger() throws Exception {

        List<String> cz = new ArrayList<>();
        //cz.add("m_advertisement");
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, username, password);
        DatabaseMetaData metaData = connection.getMetaData();

        //获取数据库下面所有表
        ResultSet tables = metaData.getTables(database, null, "%", new String[] { "TABLE" });

        while (tables.next()) {
            // 列的个数
            int columnCount = tables.getMetaData().getColumnCount();
            // 根据指定列名称获取数据
            String TABLE_NAME = tables.getString("TABLE_NAME") ;
            if (cz.contains(TABLE_NAME)) {
                continue;
            }
            if (!c.isEmpty()) {
                if (!c.contains(TABLE_NAME)) {
                    continue;
                }
            }
            System.out.println(TABLE_NAME);
            cz.add(TABLE_NAME);
            // User表  获取表的所有列
            ResultSet columns = metaData.getColumns(database, "%", TABLE_NAME, "%");

            // 表格
            List<SwaggerHeader> tableList = new ArrayList<SwaggerHeader>();

            // 表单
            List<FromAddColumn> columnList = new ArrayList<FromAddColumn>();
            StringBuilder json = new StringBuilder();
            while (columns.next()) {
                String COLUMN_NAME = columns.getString("COLUMN_NAME");
                COLUMN_NAME = convertName(COLUMN_NAME);
                String TYPE_NAME = columns.getString("TYPE_NAME");
                String REMARKS = columns.getString("REMARKS");
                String COLUMN_SIZE = columns.getString("COLUMN_SIZE");
                if (COLUMN_NAME.equals("yn")) {
                    continue;
                }
                //{"label":"编码","minWidth":"'150px'","prop":"id"};
                //name="mobile",value="手机号",required=true,paramType="form"
                json.append(",\"").append(COLUMN_NAME).append("\":null");
                SwaggerHeader th = new SwaggerHeader();
                th.setName(COLUMN_NAME);
                th.setValue(REMARKS.contains(" ") ? REMARKS.split(" ")[0]: REMARKS);
                if (COLUMN_NAME.equals("id")) {
                    th.setValue("编码");
                }
                th.setRequired(true);
                th.setParamType("form");
                tableList.add(th);
                System.out.println(th.toString());
                FromAddColumn f = new FromAddColumn();
                f.setField(COLUMN_NAME);
                f.setName(REMARKS.contains(" ") ? REMARKS.split(" ")[0]: REMARKS);
                if (COLUMN_NAME.equals("id")) {
                    f.setName("编码");
                } else if ("DATETIME".equals(TYPE_NAME)) {
                    f.setDate(true);
                    f.setPlaceholder("请选择时间");
                } else if (("BIT".equals(TYPE_NAME) || "TINYINT".equals(TYPE_NAME)) && COLUMN_SIZE.equals("1")) {
                    String str[] = REMARKS.split(" ");
                    List<Map<String, String>> selectList = new ArrayList<>();
                    for (int i = 1, l = str.length; i < l; i++) {
                        String s = str[i];
                        if (s.trim().equals("") || s.trim().equals("默认")){
                            continue;
                        }
                        Map<String, String> m = new HashMap<>();
                        String[] d = s.split(":");
                        m.put("val", d[0]);
                        m.put("label", d[1]);
                        selectList.add(m);
                    }
                    f.setSelectData(selectList);
                }
                columnList.add(f);
            }
            System.out.println("{" + json.toString().substring(1) + "}");

            System.out.println(JSON.toJSONString(tableList));
            //System.out.println(JSON.toJSONString(columnList));
        }

    }

    public static void json() throws Exception {
        List<String> cz = new ArrayList<>();
        String url = "jdbc:mysql://rm-8vb43b03vawbw24k0oo.mysql.zhangbei.rds.aliyuncs.com:3306/yfgranary?serverTimezone=GMT";
        String user = "root";
        String password = "Jiajiale5";
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, user, password);
        DatabaseMetaData metaData = connection.getMetaData();

        //获取数据库下面所有表
        ResultSet tables = metaData.getTables("yfgranary", null, "%", new String[] { "TABLE" });

        while (tables.next()) {
            // 列的个数
            int columnCount = tables.getMetaData().getColumnCount();
            // 根据指定列名称获取数据
            String TABLE_NAME = tables.getString("TABLE_NAME") ;
            if (cz.contains(TABLE_NAME)) {
                continue;
            }
            if (!c.isEmpty()) {
                if (!c.contains(TABLE_NAME)) {
                    continue;
                }
            }
            System.out.println(TABLE_NAME);
            cz.add(TABLE_NAME);
            // User表  获取表的所有列
            ResultSet columns = metaData.getColumns("yfgranary", "%", TABLE_NAME, "%");

            // 表格
            List<SwaggerHeader> tableList = new ArrayList<SwaggerHeader>();

            // 表单
            List<FromAddColumn> columnList = new ArrayList<FromAddColumn>();
            StringBuilder json = new StringBuilder();
            while (columns.next()) {
                String COLUMN_NAME = columns.getString("COLUMN_NAME");
                COLUMN_NAME = convertName(COLUMN_NAME);
                String TYPE_NAME = columns.getString("TYPE_NAME");
                String REMARKS = columns.getString("REMARKS");
                String COLUMN_SIZE = columns.getString("COLUMN_SIZE");
                if (COLUMN_NAME.equals("yn")) {
                    continue;
                }
                //{"label":"编码","minWidth":"'150px'","prop":"id"};
                //name="mobile",value="手机号",required=true,paramType="form"
                json.append(",\"").append(COLUMN_NAME).append("\":null");
                SwaggerHeader th = new SwaggerHeader();
                th.setName(COLUMN_NAME);
                th.setValue(REMARKS.contains(" ") ? REMARKS.split(" ")[0]: REMARKS);
                if (COLUMN_NAME.equals("id")) {
                    th.setValue("编码");
                }
                th.setRequired(true);
                th.setParamType("form");
                tableList.add(th);
                System.out.println(th.toString());
                FromAddColumn f = new FromAddColumn();
                f.setField(COLUMN_NAME);
                f.setName(REMARKS.contains(" ") ? REMARKS.split(" ")[0]: REMARKS);
                if (COLUMN_NAME.equals("id")) {
                    f.setName("编码");
                } else if ("DATETIME".equals(TYPE_NAME)) {
                    f.setDate(true);
                    f.setPlaceholder("请选择时间");
                } else if (("BIT".equals(TYPE_NAME) || "TINYINT".equals(TYPE_NAME)) && COLUMN_SIZE.equals("1")) {
                    String str[] = REMARKS.split(" ");
                    List<Map<String, String>> selectList = new ArrayList<>();
                    for (int i = 1, l = str.length; i < l; i++) {
                        String s = str[i];
                        if (s.trim().equals("") || s.trim().equals("默认")){
                            continue;
                        }
                        Map<String, String> m = new HashMap<>();
                        String[] d = s.split(":");
                        if(d.length > 1) {
                            m.put("val", d[0]);
                            m.put("label", d[1]);
                            selectList.add(m);
                        }
                    }
                    f.setSelectData(selectList);
                }
                columnList.add(f);
            }
            System.out.println("{" + json.toString().substring(1) + "}");

            System.out.println(JSON.toJSONString(tableList));
            //System.out.println(JSON.toJSONString(columnList));
        }

    }

}


